# 定义要搜索的根目录列表
$rootDirs = @("C:\", "D:\", "E:\")

# 定义函数以检查目录是否为空
function IsDirectoryEmpty {
    param (
        [string]$dirPath
    )
    return -not (Get-ChildItem -Path $dirPath -Force -ErrorAction SilentlyContinue)
}

# 获取所有空目录并输出路径然后删除
foreach ($rootDir in $rootDirs) {
    if (Test-Path -Path $rootDir) {
        Write-Host "Checking for empty directories on $rootDir"
        # 获取所有目录
        $allDirs = Get-ChildItem -Path $rootDir -Directory -Recurse -Force -ErrorAction SilentlyContinue

        # 遍历所有目录并检查是否为空
        foreach ($dir in $allDirs) {
            if (IsDirectoryEmpty -dirPath $dir.FullName) {
                Write-Output "Found empty directory: $($dir.FullName)"
                
                try {
                    Remove-Item -Path $dir.FullName -Recurse -Force -ErrorAction Stop
                    Write-Output "Deleted empty directory: $($dir.FullName)"
                } catch {
                    Write-Warning "Failed to delete $($dir.FullName): $_"
                }
            }
        }
    } else {
        Write-Warning "$rootDir does not exist or is not accessible."
    }
}